<?php

namespace App\Http\Controllers\Api\Appointment;

use App\Http\Controllers\Controller;
use App\Models\Appointment;
use App\Models\SchoolGate;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class AppointmentController extends Controller
{
    /**
     * 提价预约信息
     *
     * @return array
     */
    public function submit(): array
    {
        $user = auth('api')->user();
        $create = [
            'user_id'=>$user['id'],
            'username'=>$user['username'],
            'mobile'=> empty(request('mobile')) ? $user['mobile']  :  request('mobile'),
            'id_card'=>empty(request('id_card')) ? $user['id_card']  :  request('id_card'),
            'enter_at'=>request('enter_at',Carbon::now()),
            //'actual_enter_at'=>request('actual_enter_at',Carbon::now()),
            'number'=>request('number',3),
            'school_gate_id'=>request('school_gate_id',1410),
        ];
        DB::beginTransaction();
        try {
            Appointment::query()->create($create);
            DB::commit();
            return ret('提交成功!');
        } catch (\Exception $e) {
            DB::rollBack();
            return retError($e->getMessage());
        }
    }

    /**
     * 我的预约
     *
     * @return array
     */
    public function mine(): array
    {
        $user_id = auth('api')->id();
        $where = [
            'user_id'=>$user_id
        ];
        $data = Appointment::query()->where($where)->latest()->get();
        $result = array();
        if($data){
            foreach ($data as $val){
                $result[] = Appointment::getDataByID($val['id']);
            }
        }
        return ret([
            'data'=>$result
        ]);
    }

    /**
     * 校门类型
     *
     * @return array
     */
    public function schoolGate(): array
    {
        $where = array();

        $where['status'] = 1;

        $name = request('name',null);                  // 按名称搜索
        if ($name) {
            $where[] = ['name','like',"$name%"];
        }

        $first_letter = request('first_letter',null);                  // 按首字母搜索
        if ($first_letter) {
            $where['first_letter'] =$first_letter;
        }

        $SchoolGate = SchoolGate::getAllData($where);
        $data = array();
        if($SchoolGate){
            foreach ($SchoolGate as $value){
                $columns = ['id','name','first_letter'];
                $data[] = SchoolGate::getDataByID($value['id'],$columns);
            }
        }
        $result = [
            'data'=>$data
        ];
        return ret($result);
    }



    /**
     * 校门类型
     *
     * @return array
     */
    public function schoolGateLimit(Request $request) :array
    {
        $where = array();

        $where['status'] = 1;

        $name = request('name',null);                  // 按名称搜索
        if ($name) {
            $where[] = ['name','like',"$name%"];
        }

        $first_letter = request('first_letter',null);                  // 按首字母搜索
        if ($first_letter) {
            $where['first_letter'] =$first_letter;
        }

        $page = request('page',1);    //页数
        $limit =  request('limit',20);  //每页显示条数
        $offset = ($page - 1) * $limit; //偏移量


        $count = SchoolGate::query()
            ->where($where)
            ->count();

        $SchoolGate = SchoolGate::query()
            ->where($where)
            ->oldest('sort')
            ->oldest('first_letter')
            ->offset($offset)->limit($limit)
            ->get();

        $total_pages = ceil(($count + $page-1) / $limit);


        // $SchoolGate = SchoolGate::getAllData($where);
        $data = array();
        if($SchoolGate){
            foreach ($SchoolGate as $value){
                $columns = ['id','name','first_letter'];
                $data[] = SchoolGate::getDataByID($value['id'],$columns);
            }
        }
        $return['result'] = $data;
        $return['count'] = $count;
        $return['page'] = $page;
        $return['total_pages'] = $total_pages;

        return ret($return);

    }
}
